package me.itsoo.artemis.framework.repository.datasource;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * ConnectionProxy
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/19
 */
@EqualsAndHashCode
public class SqlConnectionProxy implements SqlConnection {

    private final ConnectionPool subjectPool;

    @Getter
    private final Connection sourceConnection;

    @Getter
    @Setter
    private long lastUsedTimestamp;

    /**
     * Connection 代理对象构造
     *
     * @param dataSource ConnectionPool
     * @param connection Connection
     */
    public SqlConnectionProxy(ConnectionPool dataSource, Connection connection) {
        this.subjectPool = dataSource;
        this.sourceConnection = connection;
        this.lastUsedTimestamp = System.currentTimeMillis();
    }

    @Override
    public void close() {
        subjectPool.releaseConnection(this);
    }

    @Override
    public boolean isValid() {
        try {
            return sourceConnection.isValid(0);
        } catch (SQLException e) {
            return false;
        }
    }

    @Override
    public boolean isClosed() {
        try {
            return !subjectPool.isActiveConnection(this) || sourceConnection.isClosed();
        } catch (SQLException e) {
            return true;
        }
    }

    ////////////////////////////////////
    //  Delegated to original object  //
    ////////////////////////////////////

    @Override
    public <T> T unwrap(Class<T> sourceType) throws SQLException {
        return sourceConnection.unwrap(sourceType);
    }

    @Override
    public boolean isWrapperFor(Class<?> sourceType) throws SQLException {
        return sourceConnection.isWrapperFor(sourceType);
    }

    @Override
    public Statement createStatement() throws SQLException {
        return sourceConnection.createStatement();
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return sourceConnection.prepareStatement(sql);
    }

    @Override
    public CallableStatement prepareCall(String sql) throws SQLException {
        return sourceConnection.prepareCall(sql);
    }

    @Override
    public String nativeSQL(String sql) throws SQLException {
        return sourceConnection.nativeSQL(sql);
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        sourceConnection.setAutoCommit(autoCommit);
    }

    @Override
    public boolean getAutoCommit() throws SQLException {
        return sourceConnection.getAutoCommit();
    }

    @Override
    public void commit() throws SQLException {
        sourceConnection.commit();
    }

    @Override
    public void rollback() throws SQLException {
        sourceConnection.rollback();
    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return sourceConnection.getMetaData();
    }

    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {
        sourceConnection.setReadOnly(readOnly);
    }

    @Override
    public boolean isReadOnly() throws SQLException {
        return sourceConnection.isReadOnly();
    }

    @Override
    public void setCatalog(String catalog) throws SQLException {
        sourceConnection.setCatalog(catalog);
    }

    @Override
    public String getCatalog() throws SQLException {
        return sourceConnection.getCatalog();
    }

    @Override
    public void setTransactionIsolation(int level) throws SQLException {
        sourceConnection.setTransactionIsolation(level);
    }

    @Override
    public int getTransactionIsolation() throws SQLException {
        return sourceConnection.getTransactionIsolation();
    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
        return sourceConnection.getWarnings();
    }

    @Override
    public void clearWarnings() throws SQLException {
        sourceConnection.clearWarnings();
    }

    @Override
    public Statement createStatement(int resultSetType, int concurrency) throws SQLException {
        return sourceConnection.createStatement(resultSetType, concurrency);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int concurrency) throws SQLException {
        return sourceConnection.prepareStatement(sql, resultSetType, concurrency);
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int concurrency) throws SQLException {
        return sourceConnection.prepareCall(sql, resultSetType, concurrency);
    }

    @Override
    public Map<String, Class<?>> getTypeMap() throws SQLException {
        return sourceConnection.getTypeMap();
    }

    @Override
    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
        sourceConnection.setTypeMap(map);
    }

    @Override
    public void setHoldability(int holdability) throws SQLException {
        sourceConnection.setHoldability(holdability);
    }

    @Override
    public int getHoldability() throws SQLException {
        return sourceConnection.getHoldability();
    }

    @Override
    public Savepoint setSavepoint() throws SQLException {
        return sourceConnection.setSavepoint();
    }

    @Override
    public Savepoint setSavepoint(String name) throws SQLException {
        return sourceConnection.setSavepoint(name);
    }

    @Override
    public void rollback(Savepoint savepoint) throws SQLException {
        sourceConnection.rollback(savepoint);
    }

    @Override
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        sourceConnection.releaseSavepoint(savepoint);
    }

    @Override
    public Statement createStatement(int resultSetType, int concurrency, int holdability) throws SQLException {
        return sourceConnection.createStatement(resultSetType, concurrency, holdability);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int concurrency, int holdability) throws SQLException {
        return sourceConnection.prepareStatement(sql, resultSetType, concurrency, holdability);
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int concurrency, int holdability) throws SQLException {
        return sourceConnection.prepareCall(sql, resultSetType, concurrency, holdability);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return sourceConnection.prepareStatement(sql, autoGeneratedKeys);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        return sourceConnection.prepareStatement(sql, columnIndexes);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        return sourceConnection.prepareStatement(sql, columnNames);
    }

    @Override
    public Clob createClob() throws SQLException {
        return sourceConnection.createClob();
    }

    @Override
    public Blob createBlob() throws SQLException {
        return sourceConnection.createBlob();
    }

    @Override
    public NClob createNClob() throws SQLException {
        return sourceConnection.createNClob();
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
        return sourceConnection.createSQLXML();
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
        return sourceConnection.isValid(timeout);
    }

    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {
        sourceConnection.setClientInfo(name, value);
    }

    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        sourceConnection.setClientInfo(properties);
    }

    @Override
    public String getClientInfo(String name) throws SQLException {
        return sourceConnection.getClientInfo(name);
    }

    @Override
    public Properties getClientInfo() throws SQLException {
        return sourceConnection.getClientInfo();
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        return sourceConnection.createArrayOf(typeName, elements);
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        return sourceConnection.createStruct(typeName, attributes);
    }

    @Override
    public void setSchema(String schema) throws SQLException {
        sourceConnection.setSchema(schema);
    }

    @Override
    public String getSchema() throws SQLException {
        return sourceConnection.getSchema();
    }

    @Override
    public void abort(Executor executor) throws SQLException {
        sourceConnection.abort(executor);
    }

    @Override
    public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
        sourceConnection.setNetworkTimeout(executor, milliseconds);
    }

    @Override
    public int getNetworkTimeout() throws SQLException {
        return sourceConnection.getNetworkTimeout();
    }
}
